﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class frmPwdUpdate : Form
    {
        public frmPwdUpdate()
        {
            InitializeComponent();
            if (MyGlobal.curUser.UserName == "system")
            {
                this.panel1.Visible = false;
            }
        }

        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
            {
                return;
            }
            try
            {
                MyGlobal.curUser.Password = this.txtPwd.Text.Trim();
                MyGlobal.curUser.UpdateWithKey(MyGlobal.curUser.ID);
                MessageHelper.ShowInfo("修改成功！");
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageHelper.ShowFail("修改失败！" + ex.Message);
            }
        }


        /// <summary>
        /// 检查输入
        /// </summary>
        /// <returns></returns>
        bool CheckInput()
        {
            if (this.txtYpwd.Text.Trim() == "")
            {
                MessageHelper.ShowInfo("请输入原密码！");
                return false;
            }
            if (this.txtYpwd.Text.Trim() != MyGlobal.curUser.Password)
            {
                MessageHelper.ShowInfo("原密码有误！");
                return false;

            }
            if (this.txtPwd.Text.Trim() == "")
            {
                MessageHelper.ShowInfo("请输入新密码！");
                return false;
            }
            if (this.txtPwd2.Text.Trim() == "")
            {
                MessageHelper.ShowInfo("请输入确认密码！");
                return false;
            }

            if (this.txtPwd2.Text.Trim() != this.txtPwd.Text.Trim())
            {
                MessageHelper.ShowInfo("两次密码输入不一致！");
                return false;
            }
            return true;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 用于还原密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            UserInfor ui = new UserInfor();
            DataTable dt = ui.Query("UserName='admin'");
            if (dt.Rows.Count > 0)
            {
                string id = dt.Rows[0]["编号"].ToString();
                ui.UserName = "admin";
                ui.Password = "123";
                ui.UpdateWithKey(id);
                MessageHelper.ShowInfo("修改重置成功！");
            }
        }
    }
}
